require "ISUI/ISCollapsableWindow"

QNW_TS_TaskWindow = ISCollapsableWindow:derive("QNW_TS_TaskWindow")

local this = QNW_TS_TaskWindow

function QNW_TS_TaskWindow:new(_x, _y, _width, _height, _player)
    local o = ISCollapsableWindow:new(_x, _y, _width, _height)
    setmetatable(o, self)
    self.__index = self
    o.player = _player
    o.tab = {}
    this.instance = o
    return o
end

function QNW_TS_TaskWindow:createChildren()
    ISCollapsableWindow.createChildren(self)

    self:refresh()
end

function QNW_TS_TaskWindow:refresh()
    if self.panel then
        self:removeChild(self.panel)
    end

    local th = self:titleBarHeight()
    local rh = self:resizeWidgetHeight()
    self.panel = ISTabPanel:new(0, th, self.width, self.height - th - rh)
    self.panel.anchorRight = true
    self.panel.anchorBottom = true
    self.panel.anchorTop = true
    self.panel.anchorLeft = true
    self.panel:initialise()
    self.panel.tabPadX = 15
    self.panel.equalTabWidth = true
    self.panel.allowDraggingTabs = false
    self:addChild(self.panel)

    self.tab.All = QNW_TS_TaskTabPanel:new(0, 0, self.panel.width, self.panel.height - self.panel.tabHeight, self.player)
    self.tab.All:initialise()
    self.panel:addView(getText("IGUI_QNW_TaskSystem_Window_All"), self.tab.All)
    self.tab.All:initList(QNW_TS_ClientCore.taskMap)
    for k, v in pairs(QNW_TS_ClientCore.taskCategoryMap) do
        self.tab[k] = QNW_TS_TaskTabPanel:new(0, 0, self.panel.width, self.panel.height - self.panel.tabHeight, self.player)
        self.tab[k]:initialise()
        self.panel:addView(v.name, self .tab[k])
        self.tab[k]:initList(QNW_TS_Util.tableGet(QNW_TS_ClientCore.taskMap, function(k1, v1)
            return v1.taskCategory.Type == k
        end))
    end
end

function QNW_TS_TaskWindow:destroy()
    UIManager.setShowPausedMessage(true)
    self:setVisible(false)
    self:removeFromUIManager()
end
